<?php
require_once "db.php";

$add_hall_cin = db_escape($_POST['add_hall_cin']);


if ($add_hall_cin==="choose_cinema") {
	echo "Choose cinema!";
	exit();
}

$hall_type = db_escape($_POST['hall_type']);
$num_rows = db_escape($_POST['num_rows']);
$num_col = db_escape($_POST['num_col']);
$num_seats_up = db_escape($_POST['num_seats_up']);


if ($hall_type =='rect') {
	$hall_type_num = '0';
}
elseif ($hall_type =='trapz') {
	$hall_type_num = '1';
	if ($num_rows > $num_seats_up) {
		echo 'The number of rows must be less than the number on the last row!';
		exit();
	}
}

$take_cinemaid = "SELECT * FROM cinema WHERE cinema_name = '$add_hall_cin'";
$get_cinemaid = db_query($take_cinemaid);
while ($cinemaid = mysqli_fetch_assoc($get_cinemaid)) {
		$cinema_id = $cinemaid['idcinema'];
	}
$check_hall_number = "SELECT hall_number FROM halls WHERE cinema_idcinema = '$cinema_id'";
$query_hall_number = db_query($check_hall_number);
if (mysqli_num_rows($query_hall_number)==0) {
	$hall_num = '1';
}
else{
	while ($existing_halls = mysqli_fetch_assoc($query_hall_number)) {
		$hall_num = $existing_halls['hall_number']+1;
	}
}
$query = "INSERT INTO halls (hall_number, num_of_rows, num_of_col, seats_on_upper_row, type, cinema_idcinema) VALUES ('$hall_num', '$num_rows', '$num_col', '$num_seats_up', '$hall_type_num', '$cinema_id')";
$result = db_query($query);
echo 'You have added a new hall!';
